package taskSuccessRate;

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvException;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @projectName: code-snippet
 * @className: SuccessRate
 * @description:
 * @author: JLONG
 * @date: 2024/10/22 11:06
 * @version: 1.0
 */
public class SuccessRate {
    public static void main(String[] args) {
        String LogFilePath = "/Users/jlong/IdeaProjects/code-snippet/src/main/resources/Endurance Test_panchip.csv";
        SuccessRate.statisticData(LogFilePath);
        System.out.println("=================================================");
        SuccessRate.state(LogFilePath);
    }

    public static void statisticData(String inputFile) {
        statisticData(new File(inputFile));
    }

    public static void statisticData(File inputFile) {
        Map<String, LabelData> labelDataMap = new HashMap<>();
        try {
            CSVReader reader = new CSVReader(new FileReader(inputFile));
            List<String[]> records = reader.readAll();
            for (String[] record : records) {
                if (record[1].equals("TASK_FINISHED_EVENT") && !record[6].equals("LabelID")) {
                    String labelID = record[6];
                    if (!labelDataMap.containsKey(labelID)) {
                        labelDataMap.put(labelID, new LabelData(labelID));
                        analyse(labelDataMap.get(labelID), record);
                    } else {
                        analyse(labelDataMap.get(labelID), record);
                    }
                }
            }
            reader.close();

            for (String labelID : labelDataMap.keySet()) {
                labelDataMap.get(labelID).printLabelData();
            }

        } catch (CsvException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void state(String inputFilePath) {
        state(new File(inputFilePath));
    }

    public static void state(File inputFile) {
        LabelData labelData = new LabelData("Total_Counter");
        try {
            CSVReader reader = new CSVReader(new FileReader(inputFile));
            List<String[]> records = reader.readAll();
        for (String[] record : records) {
                if (record[1].equals("TASK_FINISHED_EVENT") && !record[6].equals("LabelID")) {
                    analyse(labelData, record);
                }
            }
            reader.close();
            labelData.printLabelData();


        } catch (CsvException | IOException e) {
            throw new RuntimeException(e);
        }

    }

    private static void updateTaskRate(TaskData labelTaskData, String taskName) {
        if (taskName.equals("SUCCESSFUL")) {
            labelTaskData.addSuccessCounter();
        } else if (taskName.equals("FAILED")) {
            labelTaskData.addFailedCounter();
        }
    }

    private static void analyse(LabelData labelData, String[] record) {
        switch (record[5]) {
            case "IMAGE":
                updateTaskRate(labelData.getTaskDataList().getFirst(), record[9]);
                break;
            case "FLASHING":
                updateTaskRate(labelData.getTaskDataList().get(1), record[9]);
                break;
            case "QUERY_SENSOR":
                switch (record[17]) {
                    case "MCU_SUPPLY_VOLTAGE_SENSOR":
                        updateTaskRate(labelData.getTaskDataList().get(2), record[9]);
                        updateTaskRate(labelData.getTaskDataList().get(4), record[9]);
                        break;
                    case "BATTERY_RESISTANCE_SENSOR":
                        updateTaskRate(labelData.getTaskDataList().get(3), record[9]);
                        break;
                }
                 break;
            case "FORCE_CHANNEL_SCAN":
                updateTaskRate(labelData.getTaskDataList().get(5), record[9]);
                break;
            case "QUERY_COUNTER":
                switch (record[17]) {
                    case "TOTAL_DISPLAY_UPDATES":
                        updateTaskRate(labelData.getTaskDataList().get(6), record[9]);
                        break;
                    case "BROWNOUT_RESETS":
                        updateTaskRate(labelData.getTaskDataList().get(7), record[9]);
                        break;
                    case "SCANNED_CHANNELS_LSB":
                        updateTaskRate(labelData.getTaskDataList().get(8), record[9]);
                        break;
                    case "RESYNCS_MSB":
                        updateTaskRate(labelData.getTaskDataList().get(9), record[9]);
                        break;
                }

        }
    }
}
